import pandas as pd
import matplotlib.pyplot as plt

'''
1.利用Matplotlib绘制柱状图
'''

students = pd.read_excel('pandas8.xlsx')
name = '名称'
score = '分数'
age = '年龄'
# sort_values方法排序，inplace原地修改，ascending=False从大到小
students.sort_values(by=score, inplace=True, ascending=False)
# 绘制柱状图
plt.bar(students[name], students[score], color='orange')

# # 设置标题
# plt.title('Students Score', fontsize=16)
# # 设置x轴与y轴名称
# plt.xlabel('Name')
# plt.ylabel('Score')
# # x轴中要显示的名字太长，利用rotation将其旋转90度，方便显示
# plt.xticks(students[name], rotation='90')
# # 紧凑型布局
# plt.tight_layout()
# plt.show()

# Font Properties 文字属性
# from matplotlib.font_manager import FontProperties
# myfont = FontProperties(fname='SimHei.ttf')
# plt.title('学生分数', fontproperties=myfont, fontsize=16)
# plt.xlabel(name, fontproperties=myfont)
# plt.ylabel(score, fontproperties=myfont)
# plt.xticks(students[name], rotation='90')
# plt.tight_layout()
# plt.show()

'''
2.Pandas绘制折线图
'''

students = pd.read_excel('pandas8.xlsx')
name = '名称'
score = '分数'
age = '年龄'

# 指定默认字体
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
# # # 解决负号’-‘显示为方块的问题
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

# students.plot(y=[score, age])
# plt.title('学生分数', fontsize=16, fontweight='bold')
# plt.xticks(students.index)
# print(plt.rcParams)
# plt.show()

'''
3.Pandas绘制散点图
'''

students = pd.read_excel('pandas8_2.xlsx')
name = '名称'
weight = '体重'
height = '身高'

students.plot.scatter(x=weight, y=height)
plt.title('学生体重', fontsize=16, fontweight='bold')
plt.xlabel(weight)
plt.ylabel(height)
plt.show()